package resultx

import (
	"fmt"

	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

var message map[uint32]string

func init() {
	message = make(map[uint32]string)
	message[OK] = "SUCCESS"
	message[SERVER_COMMON_ERROR] = "服务器开小差啦，稍后再来试一试" // #24.02.18#
	message[REUQEST_PARAM_ERROR] = "请求参数错误" // ##25.02.15##
	message[TOKEN_EXPIRE_ERROR] = "token失效，请重新登录" // ##25.02.14##
	  // "Your session has expired. Please log in to continue where you left off."
	  // "您的会话已过期，请登录来继续您的工作。"
	message[TOKEN_GENERATE_ERROR] = "生成token失败"
	message[DB_ERROR] = "数据库繁忙，请稍后再试" // ##25.03.25##
	message[DB_UPDATE_AFFECTED_ZERO_ERROR] = "更新数据影响行数为0"
	message[DTM_ERROR] = "获取子事务屏障对象失败"
	message[UPLOAD_ERROR] = "上传失败"
	message[UPLOAD_WRONG_TYPE_ERROR] = "上传文件类型错误"
	message[UPLOAD_OVER_SIZE_ERROR] = "上传文件大小超出上限"

	message[USER_AUTH_PARAM_INVALID] = "用户认证参数错误" // ##25.03.10##
	message[USER_STATUS_INVALID] = "该用户已注销或被禁用"
	message[USER_EXISTS] = "该用户已存在" // #24.03.29#
	message[USER_REGISTER_FAILED] = "注册失败" // #24.03.30#
	message[USER_LOGIN_FAILED] = "登录失败"
	message[USER_GET_INFO_FAILED] = "获取用户信息失败，请重新登录" // Unable to get information for user.  // ##25.03.10##
	message[USER_ID_REQUIRED] = "用户ID不能为空" // user id is required  // ##25.04.16##
	message[USER_ID_INVALID] = "用户ID无效" // ##25.04.16##
	message[USER_LOGIN_WXMINI_FAILED] = "微信小程序登录失败" // #24.09.14#
	message[USER_AUTH_WXMINI_FAILED] = "微信小程序授权失败"
	message[USER_AUTH_MODIFY_FAILED] = "修改用户信息失败" // ##25.03.10##
	message[USER_ASSET_INVALID] = "用户数据无效" // ##25.04.16##
	message[USER_ADDRESS_CONSIGNEE_REQUIRED] = "收货人名称及手机号必填"
	message[USER_ADDRESS_GEOGRAPHIC_REQUIRED] = "请指定省市区"
	message[USER_ADDRESS_GEOGRAPHIC_INVALID] = "指定的省市区无效"
	message[USER_ADDRESS_ADDRESS_REQUIRED] = "详细地址不能为空"
	message[USER_ADDRESS_ID_REQUIRED] = "地址ID不能为空" // ##25.05.17##
	message[USER_ADDRESS_ID_INVALID] = "地址ID无效"

	message[ENTRY_PERFUME_ID_REQUIRED] = "香水ID无效" // ##25.03.12##
	message[ENTRY_NO_SEARCH_TERM_SPECIFIED] = "未指定搜索条件" // No search term specified. Showing recent items. 未指定搜索条件。自动显示最近发布条目。
	message[ENTRY_NO_ITEMS_FOUND] = "未找到搜索结果，请重新搜索。" // No media items found. Try a different search.
	message[ENTRY_PROPOSAL_CONTENT_REQUIRED] = "请输入内容" // No media items found. Try a different search.
	message[ENTRY_ID_REQUIRED] = "词条ID无效" // ##25.01.02##

	message[FORUM_POST_CONTENT_REQUIRED] = "请输入内容" // Please type your comment text. // ##25.02.14##
	message[FORUM_POST_AUTHOR_ID_INVALID] = "作者ID无效" // ##25.02.14##
	message[FORUM_POST_ID_REQUIRED] = "PostId不能为空" // ##25.03.31##
	message[FORUM_POST_ID_INVALID] = "PostId无效"
	message[FORUM_POST_STATUS_INVALID] = "已删除或状态无效" // ##25.04.01##
	message[FORUM_POST_PARENT_ID_REQUIRED] = "香水ID不能为空" // ##25.02.14##
	message[FORUM_POST_PHOTO_REQUIRED] = "请至少上传一张图片" // Please upload at least 1 photo.
	message[FORUM_POST_TITLE_REQUIRED] = "请输入标题"
	message[FORUM_POST_CONTENT_INVALID] = "内容包含敏感词" // ##25.02.14##

	message[FORUM_TOPIC_TITLE_REQUIRED] = "提问标题不能为空" // Content, title, and excerpt are empty. // ##25.02.21##
	message[FORUM_TOPIC_CONTENT_REQUIRED] = "提问内容不能为空" // Content, title, and excerpt are empty. // ##25.02.21##
	message[FORUM_TOPIC_AUTHOR_ID_INVALID] = "作者ID无效" // ##25.02.21##
	message[FORUM_TOPIC_ID_REQUIRED] = "TopicId不能为空" // ##25.02.25##
	message[FORUM_TOPIC_ID_INVALID] = "TopicId无效"
	message[FORUM_TOPIC_STATUS_INVALID] = "已删除或状态无效"
	message[FORUM_TOPIC_CONTENT_INVALID] = "内容包含敏感词" // ##25.02.21##

	message[FORUM_REACTION_CONTENT_ID_INVALID] = "操作对象ID无效" // ##25.01.02##
	message[FORUM_REACTION_USER_ID_INVALID] = "用户ID无效" // ##25.01.02##
	message[FORUM_REACTION_TYPE_INVALID] = "无效的操作类型" // Invalid post type. // ##25.03.12##

	message[FORUM_PERFUME_CABINET_USER_ID_REQUIRED] = "用户ID不能为空" // ##25.05.20##
	message[FORUM_PERFUME_CABINET_OBJECT_ID_REQUIRED] = "收藏对象ID不能为空" // ##25.05.20##
	message[FORUM_PERFUME_CABINET_OBJECT_TYPE_REQUIRED] = "收藏类型不能为空" // ##25.05.20##
	message[FORUM_PERFUME_CABINET_ID_REQUIRED] = "收藏ID不能为空" // ##25.05.20##

	message[PRODUCT_VARIATION_ID_REQUIRED] = "请选择至少一个产品" // #24.08.27#
	message[PRODUCT_NO_VALID_VARIATION_ID] = "产品SKU ID无效"
	message[PRODUCT_VARIATION_STOCK_VALIDATE_FAIL] = "产品SKU库存验证失败" // ##25.05.12##
	message[PRODUCT_VARIATION_STOCK_INVALID] = "产品SKU库存异常" // insufficient stock // ##25.05.12##

	message[ADD_CART_ITEM_FAILED] = "添加失败，库存不足"
	message[UPDATE_CART_ITEM_FAILED] = "修改失败，库存不足" // ##24.10.26##

	message[PRODUCT_PACKAGE_ID_REQUIRED] = "购物金ID不能为空"
	message[PRODUCT_PACKAGE_ID_INVALID] = "购物金ID无效"
	message[PRODUCT_ID_REQUIRED] = "产品ID不能为空" // #24.07.22#
	message[PRODUCT_ID_INVALID] = "产品ID无效" // #24.07.22#

	message[ORDER_SN_REQUIRED] = "订单编号不能为空"
	message[ORDER_SN_EXISTS] = "重复的订单编号"
	message[ORDER_PARAM_INVALID] = "订单参数错误" // #24.10.06#
	message[ORDER_ID_REQUIRED] = "请指定至少一个订单ID" // ##25.05.06##
	message[ORDER_ID_INVALID] = "订单ID无效" // ##25.05.06##
	message[ORDER_PAYMENT_ID_REQUIRED] = "请选择支付方式"
	message[ORDER_PAYMENT_ID_INVALID] = "支付方式无效" // ##25.05.17##
	message[ORDER_REFUND_ID_INVALID] = "退款ID无效"
	message[ORDER_SHIPMENT_ID_REQUIRED] = "请选择发货类型"
	message[ORDER_SHIPMENT_ID_INVALID] = "发货类型无效" // ##25.05.17##
	message[ORDER_NOT_FOUND] = "未找到订单"
	message[ORDER_TRADE_STATE_INVALID] = "订单状态错误" // ##25.05.06##
	message[ORDER_CREATE_FAILED] = "创建订单失败"
	message[ORDER_UPDATE_TRADE_STATE_FAILED] = "修改订单状态失败"
	message[ORDER_UPDATE_CART_FAILED] = "修改购物车失败" // #24.08.27#

	message[AFTER_SALE_PARAM_INVALID] = "售后参数错误" // ##25.05.07## Todo: 可以去除？
	message[AFTER_SALE_ID_REQUIRED] = "需要售后ID" // ##25.05.07##
	message[AFTER_SALE_ID_INVALID] = "售后ID无效" // ##25.05.07##

	message[ACTIVITY_NOT_STARTED] = "活动尚未开始" // ##25.05.13##
	message[ACTIVITY_ENDED] = "活动已结束" // ##25.05.13##
	message[ACTIVITY_MEMBER_RESTRICTED] = "该活动对当前用户不适用" // ##25.05.13##
	message[ACTIVITY_ID_REQUIRED] = "活动ID不能为空"
	message[ACTIVITY_ID_INVALID] = "活动ID无效"

	message[PROMOTION_STATUS_INVALID] = "优惠状态无效"

	message[COUPON_NOT_STARTED] = "优惠券尚未开始使用"
	message[COUPON_ENDED] = "优惠券已过期"
	message[COUPON_MEMBER_RESTRICTED] = "该优惠券对当前用户不适用"
	message[COUPON_ID_REQUIRED] = "优惠券ID不能为空" // ##25.04.14##
	message[COUPON_ID_INVALID] = "优惠券ID无效"
	message[COUPON_OVERSIZE_ERROR] = "领取次数超出上限"

	message[INTEGRAL_TASK_FINISHES_NUMBER_EXCEED] = "积分任务完成次数超出上限"
	message[INTEGRAL_TASK_TASK_ID_REQUIRED] = "积分任务任务ID不能为空"
	message[INTEGRAL_TASK_INTEGRALS_REQUIRED] = "积分任务积分数量不能为空"
	message[INTEGRAL_TASK_INTEGRALS_INVALID] = "积分任务积分有误"
	message[INTEGRAL_TASK_NAME_REQUIRED] = "积分任务任务名称不能为空"

	message[PAYMENT_WECHAT_PAY_INIT_FAIL] = "微信支付初始化失败-私钥错误"            // wechat pay fail - wechat pay init fail - mchPrivateKey err
	message[PAYMENT_WECHAT_PAY_INIT_CLIENT_FAIL] = "微信支付初始化失败-初始化客户端错误" // new wechat pay client err
	message[PAYMENT_WECHAT_PAY_INIT_PRE_ORDER_FAIL] = "微信支付初始化失败-预支付订单号错误"
	message[PAYMENT_WECHAT_PAY_CALLBACK_FAIL] = "微信支付回调失败" // wechat pay callback fail - Failed to parse data
	message[PAYMENT_WECHAT_REFUND_FAIL] = "微信支付退款失败"
	message[PAYMENT_WECHAT_REFUND_CALLBACK_FAIL] = "微信支付退款回调失败"
	message[PAYMENT_AUTO_CLOSE_ORDER_PARAM_INVALID] = "自动关闭未支付订单参数错误"
	message[PAYMENT_AUTO_CLOSE_ORDER_FAILED] = "自动关闭未支付订单失败" // #24.09.28#
	message[PAYMENT_NOTIFY_USER_PARAM_INVALID] = "支付成功通知用户参数错误"
	message[PAYMENT_NOTIFY_USER_FAILED] = "支付成功通知用户失败"

	message[LOCAL_CACHE_INIT_FAIL] = "本地缓存初始化失败"
	message[ELASTIC_INIT_CLIENT_FAIL] = "全文搜索初始化失败-初始化客户端错误" // elasticsearch connection err - creating elasticsearch client err
	message[ELASTIC_INIT_FAIL] = "全文搜索初始化失败"
	message[ELASTIC_DOCUMENT_FAIL] = "全文搜索创建文档失败" // elasticsearch indexing document err
}

func MapErrMsg(errCode uint32) string { // #24.02.24#
	if msg, ok := message[errCode]; ok {
		return msg
	}

	return "服务器开小差啦，稍后再来试一试"
}

func IsCodeErr(errCode uint32) bool { // #24.02.18#
	if _, ok := message[errCode]; ok {
		return true
	}

	return false
}

func StatusError(errCode uint32, msg string, args ...any) error { // #24.02.24#
	if len(msg) == 0 {
		msg = MapErrMsg(errCode)
	}

	if len(args) > 0 {
		msg = fmt.Sprintf(msg, args...)
	}

	return status.Error(codes.Code(errCode), msg)
}
